#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<unordered_map>

using namespace std;

int maximumCostSubstring(string s, string chars, vector<int>& vals) {
    unordered_map<char, int> val;
    for (int i = 1; i <= 26; i++)  val['a' + i - 1] = i;
    for (int i = 0; i < chars.size(); i++) val[chars[i]] = vals[i];

    int n = s.size();
    s = ' ' + s;
    vector<int> f(n + 1, INT_MIN);
    f[0] = 0;
    int res = 0;
    for (int i = 1; i <= n; i++)
    {
        int v = val[s[i]];
        f[i] = max(f[i - 1], 0) + v;
        res = max(res, f[i]);
    }
    return res;
}